package com.longrise.android.byjk.plugins.dealsituation.course.video2.education.picedu;

import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.drawable.GlideDrawable;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;
import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.widget.view.ZoomImageView;


/**
 * Created by godliness on 2017/8/1.
 */

public class PicAdapter extends PagerAdapter {

    private static final String TAG = "PicAdapter";

    private EntityBean[] mPic;

    private LayoutInflater mInflater;

    public PicAdapter(PictureActivity cxt) {
        this.mInflater = cxt.getLayoutInflater();
    }

    public void setData(EntityBean[] picItems) {
        this.mPic = picItems;
    }

    private void options(ImageView iv, ProgressBar pb, int currentPosition) {
        EntityBean picItem = mPic[currentPosition];
        loadImage(iv, pb, picItem.getString("psource"));
    }

    /**
     * 加载图片
     */
    private void loadImage(final ImageView iv, final ProgressBar pb, final String url) {
        Glide.with(iv.getContext()).load(url).placeholder(R.drawable.bg_error_2).fitCenter().into(new SimpleTarget<GlideDrawable>() {

            @Override
            public void onLoadStarted(Drawable placeholder) {
                pb.setVisibility(View.VISIBLE);
            }

            @Override
            public void onResourceReady(GlideDrawable resource, GlideAnimation<? super GlideDrawable> glideAnimation) {
                pb.setVisibility(View.GONE);
                iv.setImageDrawable(resource);
                iv.setEnabled(true);
            }

            @Override
            public void onLoadFailed(Exception e, Drawable errorDrawable) {
                pb.setVisibility(View.GONE);
                iv.setImageDrawable(errorDrawable);
            }
        });
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {
        View view = mInflater.inflate(R.layout.item_picact, null, false);
        ImageView itemView = (ZoomImageView) view.findViewById(R.id.iv_itempic);
        itemView.setEnabled(false);
        ProgressBar pb = (ProgressBar) view.findViewById(R.id.pb_itempic);
        options(itemView, pb, position);
        container.addView(view);
        return view;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        View itemView = (View) object;
        container.removeView(itemView);
    }

    @Override
    public int getCount() {
        return mPic == null ? 0 : mPic.length;
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

}

